package com.financing.wap.service.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.jfree.util.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.financing.wap.common.SessionUtil;
import com.financing.wap.dao.IActivitiesMollionsWealthDao;
import com.financing.wap.dao.IProductDao;
import com.financing.wap.dao.IRecommentProductDao;
import com.financing.wap.dto.ActivitesMollionsWealthDTO;
import com.financing.wap.dto.FpFundInfoDTO;
import com.financing.wap.dto.ProductAnnualRevenueDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IProductService;
import com.financing.wap.service.IRecommentProductService;
import com.financing.wap.vo.ProductProfitAndRebateVO;
import com.financing.wap.vo.RecommentProductVO;

@Service("recommentProductServiceImpl")
public class RecommentProductServiceImpl implements IRecommentProductService {

	Logger logger = Logger.getLogger(getClass());

	@Autowired
	IRecommentProductDao recommentProductDao;
	
	@Autowired
	IProductService productService;

	
	@Autowired
	private IActivitiesMollionsWealthDao activitiesMollionsWealthDao;
	
	@Autowired
	IProductDao productDao;
	
	@Override
	public List<RecommentProductVO> queryRecommentProduct(String recommentType) throws SQLException {
		 List<RecommentProductVO> queryRecommentProduct = recommentProductDao.queryRecommentProduct(recommentType);
		 for (int i = 0;queryRecommentProduct!=null &&  i < queryRecommentProduct.size(); i++) {
			 RecommentProductVO recommentProductVO = queryRecommentProduct.get(i);
			 if("01".equals(recommentProductVO.getpType())){
				 FpFundInfoDTO fpFundInfoDTO = recommentProductVO.getFpFundInfoDTO();
				 try {
					//产品列表-新增 活动关联
					ActivitesMollionsWealthDTO activitesMollionsWealthDTO = new ActivitesMollionsWealthDTO();
					activitesMollionsWealthDTO.setpId(fpFundInfoDTO.getId());
					activitesMollionsWealthDTO.setpType("01");
					fpFundInfoDTO.setActivitiesMollionsWealthList(activitiesMollionsWealthDao.queryActivitiesMollionsWealthList(activitesMollionsWealthDTO));
				} catch (Exception e) {
					logger.error("getProductList queryActivitiesMollionsWealthList error",e);
				}
				 break;
			 }
			 try {
				List<ProductProfitAndRebateVO> productProfitAndRebateList = productService.getProductProfitAndRebateList(recommentProductVO.getProductDTO().getId(),recommentProductVO.getProductDTO().getProjectCorpType());
				UserDTO user= SessionUtil.getSession().getUser();
				//有没有权限看   返佣
				boolean flagLookRebate=!(user==null||user.getId()==null||"1".equals(user.getRole()) || "99".equals(user.getRole()) || "98".equals(user.getRole()));
				for (ProductProfitAndRebateVO productProfitAndRebateVO : productProfitAndRebateList) {
					if(!flagLookRebate){
						productProfitAndRebateVO.setRebate(null);
						productProfitAndRebateVO.setpRebateAmount(null);
					}
				}
				recommentProductVO.getProductDTO().setProfitAndRebateList(productProfitAndRebateList);
				
				
//				ProductAnnualRevenueDTO profit = productDao.queryAnnualRevenue(recommentProductVO.getProductDTO().getId(),"l");
				ProductAnnualRevenueDTO profitHight = productDao.queryAnnualRevenue(recommentProductVO.getProductDTO().getId(),"h");
//				ProductAnnualRevenueDTO rebateProfit = productDao.queryAnnualRevenue(recommentProductVO.getProductDTO().getId(),"rl");
				ProductAnnualRevenueDTO rebateProfitHight = productDao.queryAnnualRevenue(recommentProductVO.getProductDTO().getId(),"rh");
				
				if(!flagLookRebate){
					if(profitHight!=null){
						profitHight.setRebate(null);
						profitHight.setpRebateAmount(null);
					}
				}else{
					if(rebateProfitHight!=null){
						profitHight.setRebate(rebateProfitHight.getRebate());
					}
				}
				
				recommentProductVO.getProductDTO().setProfitHight(profitHight);
				
				
				//产品列表-新增 活动关联
				ActivitesMollionsWealthDTO activitesMollionsWealthDTO = new ActivitesMollionsWealthDTO();
				activitesMollionsWealthDTO.setpId(recommentProductVO.getProductDTO().getId());
				activitesMollionsWealthDTO.setpType(recommentProductVO.getProductDTO().getType());
				try {
					recommentProductVO.getProductDTO().setActivitiesMollionsWealthList(activitiesMollionsWealthDao.queryActivitiesMollionsWealthList(activitesMollionsWealthDTO));
				} catch (Exception e) {
					logger.error("getProductList queryActivitiesMollionsWealthList error",e);
				}
				
			} catch (Exception e) {
				Log.error("ProductProfitAndRebateVO eror",e);
			}
		}
		 
		 return queryRecommentProduct;
	}


}
